package com.duoduo.双指针;

import com.duoduo.ListNode;

/**
 * @program: algorithm
 * @description: 删除链表的倒数第N个结点
 * 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
 * 输入：head = [1,2,3,4,5], n = 2
 * 输出：[1,2,3,5]
 * @author: chaidl  ！
 * @create: 2022-03-17 20:44
 */
public class 删除链表的倒数第N个结点 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode first=new ListNode(0,head);
        ListNode fast=head;
        ListNode slow=first;
        for (int i = 0; i < n; i++) {
            fast=fast.next;
        }
        while (fast!=null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next=slow.next.next;
        ListNode result=first.next;
        return result;
    }
}
